home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Applications / Nuntius 1.2 / src / Nuntius / UPrefsDatabase.cp < prev    next >
Encoding:
Text File  |  1994-03-14  |  15.4 KB  |  675 lines  |  [TEXT/MPS ]

  1. // Copyright © 1992 Peter Speck, speck@dat.ruc.dk. All rights reserved.
  2. // UPrefsDatabase.cp
  3.  
  4. #include "UPrefsDatabase.h"
  5. #include "UDynDynArray.h"
  6. #include "Tools.h"
  7. #include "StreamTools.h"
  8.  
  9. #include <RsrcGlobals.h>
  10. #include <ErrorGlobals.h>
  11.  
  12. #include <Errors.h>
  13. #include <Folders.h>
  14. #include <Fonts.h>
  15.  
  16. #pragma segment MyTools
  17.  
  18. #define qDebugPrefs qDebug & 0
  19.  
  20. const long kCurrentPrefsVersion = 10;
  21. const long kMinPrefsVersion = 10;
  22.  
  23. PPrefsDatabase *gPrefs = nil;
  24. //-----------------------------------------------------------------------------
  25. PPrefsDatabase::PPrefsDatabase()
  26.     : PPtrObject()
  27. {
  28.     fPrefsDB = nil;
  29.     fPrefsTypes = nil;
  30.     fIsDirty = false;
  31. }
  32.  
  33. void PPrefsDatabase::IPrefsDatabase()
  34. {
  35.     FailInfo fi;
  36.     if (fi.Try())
  37.     {
  38.         PDynDynArray *ddList = new PDynDynArray();
  39.         ddList->IDynDynArray(1024);
  40.         fPrefsDB = ddList;
  41.  
  42.         TLongintList *lList = new TLongintList();
  43.         lList->ILongintList();
  44.         fPrefsTypes = lList;
  45.         
  46.         gPrefs = this;
  47.         fi.Success();
  48.     }
  49.     else // fail
  50.     {
  51.         FreeIfPtrObject(this);
  52.         fi.ReSignal();
  53.     }
  54. }
  55.  
  56. PPrefsDatabase::~PPrefsDatabase()
  57. {
  58.     if (!gPrefs)
  59.     {
  60. #if qDebug
  61.         ProgramBreak("gPrefs == nil");
  62. #endif
  63.         return;
  64.     }
  65.     gPrefs = nil;
  66.     FreeIfPtrObject(fPrefsDB); fPrefsDB = nil;
  67.     FreeIfObject(fPrefsTypes); fPrefsTypes = nil;
  68. }
  69.  
  70. void PPrefsDatabase::DoRead(TStream *aStream)
  71. {
  72.     long version = aStream->ReadLong();
  73.     if (!MyCheckVersion(version, kMinPrefsVersion, kCurrentPrefsVersion, "PPrefsDatabase.Real"))
  74.     {
  75.         gApplication->ShowError(errIncompatiblePrefsFileFormat, messageUsePrefs);
  76.         return;
  77.     }
  78.     ReadDynamicArray(aStream, fPrefsTypes);
  79.     fPrefsDB->DoRead(aStream);
  80. #if qDebugPrefs
  81.     fprintf(stderr, "After reading pref's from disk:\n");
  82.     DumpTable();
  83. #endif
  84.     fIsDirty = false;
  85.     while (fPrefsDB->GetSize() > fPrefsTypes->fSize)
  86.         fPrefsDB->DeleteElementAt(fPrefsDB->GetSize());
  87. }
  88.  
  89. void PPrefsDatabase::DoWrite(TStream *aStream)
  90. {
  91.     aStream->WriteLong(kCurrentPrefsVersion);
  92.     WriteDynamicArray(aStream, fPrefsTypes);
  93.     fPrefsDB->DoWrite(aStream);
  94.     fIsDirty = false;
  95. }
  96.  
  97. long PPrefsDatabase::NeededDiskSpace()
  98. {
  99.     return    sizeof(long) + // version
  100.                     MyStreamSizeOfDynamicArray(fPrefsTypes) +
  101.                     fPrefsDB->NeededDiskSpace();
  102. }
  103.  
  104. void PPrefsDatabase::DeleteAll()
  105. {
  106.     fPrefsDB->DeleteAll();
  107.     fPrefsTypes->DeleteAll();
  108. }
  109.  
  110. //======== SUPPORT ==========================================================
  111. void PPrefsDatabase::SetDirtyFlag()
  112. {
  113.     if (!fIsDirty)
  114.         fIsDirty = true;
  115. }
  116.  
  117. Boolean PPrefsDatabase::IsDirty()
  118. {
  119.     return fIsDirty;
  120. }
  121.  
  122. inline long PPrefsDatabase::RoundSize(long size)
  123. {
  124.     return (size + 3) & ~3;
  125. }
  126.  
  127. long PPrefsDatabase::FindIndex(OSType id, Boolean failIfMissing)
  128. {
  129.     long noTypes = fPrefsTypes->GetSize();
  130.     if (noTypes)
  131.     {
  132.         OSType *typeP = (OSType*) fPrefsTypes->ComputeAddress(1);
  133.         for (long index = 1; index <= noTypes; index++)
  134.             if (*typeP++ == id)
  135.                 return index;
  136.     }
  137.     if (failIfMissing)
  138.     {
  139. #if qDebug
  140.         fprintf(stderr, "Prefs entry not found for type %s\n", OSType2String(id));
  141. #endif
  142.         FailOSErr(errNoSuchPrefs);
  143.     }
  144.     return kEmptyIndex;
  145. }
  146.  
  147. Ptr PPrefsDatabase::ComputePrefsAddress(OSType id)
  148. {
  149.     return fPrefsDB->ComputeAddress(FindIndex(id));
  150. }
  151.  
  152. void PPrefsDatabase::DumpTable()
  153. {
  154. #if qDebug
  155.     fprintf(stderr, "Dumping preferences table:\n");
  156.     for (long index = 1; index <= fPrefsTypes->GetSize(); index++)
  157.     {
  158.         Ptr p = fPrefsDB->ComputeAddress(index);
  159.         long data = *( (long*) p );
  160.         long offset = p - fPrefsDB->ComputeAddress(1);
  161.         fprintf(stderr, "%3ld, id = %s, data-size =%3ld, first 4 bytes = %8lx, offset = %ld\n", 
  162.             index, OSType2String(fPrefsTypes->At(index)), 
  163.             fPrefsDB->GetElementSize(index), data, offset);
  164.     }
  165.     fprintf(stderr, "End of dump\n");
  166. #endif
  167. }
  168.  
  169. #if qDebug
  170. void PPrefsDatabase::ReportWrongSize(OSType id, long size)
  171. {
  172.     ArrayIndex index = FindIndex(id);
  173.     fprintf(stderr, "Wrong element size (%ld) found, %ld expected\n",
  174.         fPrefsDB->GetElementSize(index), size);
  175.     fprintf(stderr, "index = %ld for id: %s\n", index, OSType2String(id));
  176.     DumpTable();
  177.     ProgramBreak(gEmptyString);
  178.     fPrefsDB->SetElementSize(index, size);
  179. }
  180. #else
  181. void PPrefsDatabase::ReportWrongSize(OSType /* id */, long /* size */)
  182. {
  183. }
  184. #endif
  185.  
  186. #if qDebug
  187. inline void PPrefsDatabase::DebugCheckSize(OSType id, long size)
  188. {
  189.     long realSize = fPrefsDB->GetElementSize(FindIndex(id));
  190.     if (realSize != RoundSize(size))
  191.         ReportWrongSize(id, size);            
  192. }
  193. #else
  194. inline void PPrefsDatabase::DebugCheckSize(OSType /* id */, long /* size */)
  195. {
  196. }
  197. #endif
  198.  
  199. void PPrefsDatabase::SetPrefs(OSType id, const void *dataP, long size)
  200. {
  201. #if qDebugPrefs
  202.     fprintf(stderr, "\nSetting data id %s with size %ld\n", OSType2String(id), size);
  203.     if (size != RoundSize(size))
  204.         fprintf(stderr, "- size rounded to %ld\n", size, RoundSize(size));
  205. #endif
  206.     size = RoundSize(size);
  207.     ArrayIndex index = FindIndex(id, false);
  208.     if (index == kEmptyIndex)
  209.     {
  210.         index = fPrefsDB->CreateNewElement(size);
  211.         fPrefsTypes->InsertLast(id);
  212.         fPrefsTypes->GetSize();
  213.         SetDirtyFlag();
  214.     }
  215.     if (fPrefsDB->GetElementSize(index) != size)
  216.     {
  217.         SetDirtyFlag(); // changed size
  218.         fPrefsDB->SetElementSize(index, size);
  219.     }
  220.     if (dataP == nil || size == 0)
  221.         return;
  222.     Ptr toP = fPrefsDB->ComputeAddress(index);
  223.     if (memcmp(dataP, toP, int(size)) == 0)
  224.         return; // didn't change contents
  225.     BytesMove(dataP, toP, size);
  226.     SetDirtyFlag();
  227. }
  228.  
  229. void PPrefsDatabase::GetPrefs(OSType id, void *data)
  230. {
  231.     ArrayIndex index = FindIndex(id);
  232.     long size = fPrefsDB->GetElementSize(index);
  233.     Ptr fromP = fPrefsDB->ComputeAddress(index);
  234.     BytesMove(fromP, data, size);
  235. }
  236.  
  237. long PPrefsDatabase::GetPrefsSize(OSType id)
  238. {
  239.     ArrayIndex index = FindIndex(id);
  240.     return fPrefsDB->GetElementSize(index);
  241. }
  242.  
  243. void PPrefsDatabase::DeletePrefs(OSType id)
  244. {
  245.     ArrayIndex index = FindIndex(id, false);
  246.     if (index == kEmptyIndex)
  247.         return;
  248.     fPrefsDB->DeleteElementAt(index);
  249.     fPrefsTypes->DeleteElementsAt(index, 1);
  250.     SetDirtyFlag();
  251. }
  252.  
  253. Boolean PPrefsDatabase::PrefExists(OSType id)
  254. {
  255.     return FindIndex(id, false) != kEmptyIndex;
  256. }
  257.  
  258. Boolean PPrefsDatabase::GetBooleanPrefs(OSType id)
  259. {
  260.     DebugCheckSize(id, sizeof(long));
  261.     long value = *( (long*) ComputePrefsAddress(id));
  262.     return Boolean(value);
  263. }
  264.  
  265. void PPrefsDatabase::SetBooleanPrefs(OSType id, Boolean b)
  266. {
  267.     long li = b;
  268.     SetPrefs(id, &li, sizeof(long));
  269. }
  270.  
  271. short PPrefsDatabase::GetShortPrefs(OSType id)
  272. {
  273.     DebugCheckSize(id, sizeof(long));
  274.     return short( *( (long*) ComputePrefsAddress(id)) );
  275. }
  276.  
  277. void PPrefsDatabase::SetShortPrefs(OSType id, short i)
  278. {
  279.     long li = i;
  280.     SetPrefs(id, &li, sizeof(long));
  281. }
  282.  
  283. long PPrefsDatabase::GetLongPrefs(OSType id)
  284. {
  285.     DebugCheckSize(id, sizeof(long));
  286.     return *( (long*) ComputePrefsAddress(id));
  287. }
  288.  
  289. void PPrefsDatabase::SetLongPrefs(OSType id, long l)
  290. {
  291.     SetPrefs(id, &l, sizeof(long));
  292. }
  293.  
  294. void PPrefsDatabase::GetVPointPrefs(OSType id, VPoint &vp)
  295. {
  296.     DebugCheckSize(id, sizeof(VPoint));
  297.     vp = *( (VPoint*) ComputePrefsAddress(id));
  298. }
  299.  
  300. void PPrefsDatabase::SetVPointPrefs(OSType id, const VPoint &const vp)
  301. {
  302.     SetPrefs(id, &vp, sizeof(VPoint));
  303. }
  304.  
  305. void PPrefsDatabase::GetVRectPrefs(OSType id, VRect &vr)
  306. {
  307.     DebugCheckSize(id, sizeof(VRect));
  308.     vr = *( (VRect*) ComputePrefsAddress(id));
  309. }
  310.  
  311. void PPrefsDatabase::SetVRectPrefs(OSType id, const VRect &const vr)
  312. {
  313.     SetPrefs(id, &vr, sizeof(VRect));
  314. }
  315.  
  316. void PPrefsDatabase::GetSilentVRectPrefs(OSType id, VRect &vr)
  317. {
  318.     FailInfo fi;
  319.     if (fi.Try())
  320.     {
  321.         GetVRectPrefs(id, vr);
  322.         fi.Success();
  323.         return;
  324.     }
  325.     else // fail
  326.     {
  327.         if (fi.error != errNoSuchPrefs)
  328.             fi.ReSignal();
  329.     }
  330.     vr = VRect(0, 0, 0, 0);
  331.     SetVRectPrefs(id, vr);
  332. }
  333.  
  334.  
  335. void PPrefsDatabase::GetTextStylePrefs(OSType id, TextStyle &theTextStyle)
  336. {
  337.     FailInfo fi;
  338.     if (fi.Try())
  339.     {
  340.         MATextStyle *matsP;
  341.         DebugCheckSize(id, sizeof(MATextStyle));
  342.         matsP = (MATextStyle*) ComputePrefsAddress(id);
  343.         theTextStyle.tsFace = matsP->tsFace;
  344.         theTextStyle.tsSize = matsP->tsSize;
  345.         theTextStyle.tsColor = matsP->tsColor;
  346.         CStr255 name(matsP->tsFont);
  347.         theTextStyle.tsFont = GetFontNum(name);        
  348.         fi.Success();
  349.     }
  350.     else // fail
  351.     {
  352.         if (fi.error != errNoSuchPrefs)
  353.             fi.ReSignal();
  354.         theTextStyle = gApplicationStyle;
  355.     }
  356. }
  357.  
  358. void PPrefsDatabase::SetTextStylePrefs(OSType id, const TextStyle &theTextStyle)
  359. {
  360.     MATextStyle mats;
  361.     mats.tsFace = theTextStyle.tsFace;
  362.     mats.tsSize = theTextStyle.tsSize;
  363.     mats.tsColor = theTextStyle.tsColor;
  364.     CStr255 name;
  365.     GetFontName(theTextStyle.tsFont, name);
  366.     CopyCString2String(name, mats.tsFont);
  367.     SetPrefs(id, &mats, sizeof(MATextStyle));
  368. }
  369.  
  370. OSType PPrefsDatabase::GetSignaturePrefs(OSType id)
  371. {
  372.     DebugCheckSize(id, sizeof(OSType));
  373.     return *( (OSType*) ComputePrefsAddress(id));
  374. }
  375.  
  376. void PPrefsDatabase::SetSignaturePrefs(OSType id, const OSType signature)
  377. {
  378.     SetPrefs(id, &signature, sizeof(OSType));
  379. }
  380.  
  381. void PPrefsDatabase::GetStringPrefs(OSType id, CStr255 &prefs)
  382. {
  383.     Ptr p = ComputePrefsAddress(id);
  384.     long len = CStringPtr(p)->Length();
  385.     long size = (len + 8) & ~7;
  386.     DebugCheckSize(id, size);
  387.     BytesMove(p, &prefs, len + 1);
  388. }
  389.  
  390. void PPrefsDatabase::SetStringPrefs(OSType id, const CStr255 &prefs)
  391. {
  392.     long size = (prefs.Length() + 8) & ~7; // remember length byte
  393.     SetPrefs(id, &prefs, size);
  394. }
  395.  
  396. void PPrefsDatabase::SetStringPrefs(OSType id, const char *prefs)
  397. {
  398.     CStr255 s(prefs);
  399.     SetStringPrefs(id, s);
  400. }
  401.  
  402. Handle PPrefsDatabase::GetHandlePrefs(OSType id)
  403. {
  404.     long size = *( (long*) ComputePrefsAddress(id));    
  405.     DebugCheckSize(id, sizeof(long) + size);
  406.     Handle h = NewPermHandle(size); // can move mem
  407.     Ptr p = ComputePrefsAddress(id);
  408.     BytesMove(p + sizeof(long), *h, size);
  409.     return h;
  410. }
  411.  
  412. void PPrefsDatabase::SetHandlePrefs(OSType id, Handle h)
  413. {
  414.     long size = GetHandleSize(h);
  415.     SetPrefs(id, nil, size + 4);
  416.     Ptr prefP = ComputePrefsAddress(id);
  417.     long *lP = (long*)prefP;
  418.     Ptr toP = prefP + sizeof(long);
  419.     Boolean changed = (*lP != size);
  420.     *lP = size;
  421.     if (!changed && memcmp(*h, toP, int(size)) != 0)
  422.         changed = true;
  423.     BytesMove(*h, toP, size);
  424.     if (changed)
  425.         SetDirtyFlag();
  426. }
  427.  
  428. Ptr PPrefsDatabase::GetPtrPrefs(OSType id)
  429. {
  430.     long size = *( (long*) ComputePrefsAddress(id));    
  431.     DebugCheckSize(id, sizeof(long) + size);
  432.     Ptr p = NewPermPtr(size); // can move mem
  433.     Ptr fromP = ComputePrefsAddress(id);
  434.     BytesMove(fromP + sizeof(long), p, size);
  435.     return p;
  436. }
  437.  
  438. void PPrefsDatabase::SetPtrPrefs(OSType id, Ptr p)
  439. {
  440.     long size = GetPtrSize(p);
  441.     SetPrefs(id, nil, size + 4);
  442.     Ptr prefP = ComputePrefsAddress(id);
  443.     long *lP = (long*)prefP;
  444.     Ptr toP = prefP + sizeof(long);
  445.     Boolean changed = (*lP != size);
  446.     *lP = size;
  447.     if (!changed && memcmp(p, toP, int(size)) != 0)
  448.         changed = true;
  449.     BytesMove(p, toP, size);
  450.     if (changed)
  451.         SetDirtyFlag();
  452. }
  453.  
  454. AliasHandle PPrefsDatabase::GetAliasHandlePrefs(OSType id)
  455. {
  456.     return AliasHandle(GetHandlePrefs(id));
  457. }
  458.  
  459.  
  460. void PPrefsDatabase::SetAliasHandlePrefs(OSType id, AliasHandle ah)
  461. {
  462.     SetHandlePrefs(id, Handle(ah));
  463. }
  464.  
  465. void PPrefsDatabase::GetAliasPrefs(OSType id, FSSpec &spec)
  466. {
  467.     AliasHandle ah = AliasHandle(GetHandlePrefs(id));
  468.     VOLATILE(ah);
  469.     FailInfo fi;
  470.     if (fi.Try())
  471.     {
  472.         Boolean wasChanged;
  473.         short err = ResolveAlias(nil, ah, spec, wasChanged);
  474.         if (wasChanged)
  475.             SetHandlePrefs(id, Handle(ah));
  476.         FailOSErr(err);
  477.         DisposeIfHandle(Handle(ah)); ah = nil;
  478.         fi.Success();
  479.     }
  480.     else // fail
  481.     {
  482.         DisposeIfHandle(Handle(ah)); ah = nil;
  483.         fi.ReSignal();
  484.     }
  485. }
  486.  
  487. Boolean PPrefsDatabase::TryGetAliasPrefs(OSType id, FSSpec &spec)
  488. {
  489.     FailInfo fi;
  490.     if (fi.Try())
  491.     {
  492.         GetAliasPrefs(id, spec);
  493.         fi.Success();
  494.         return true;
  495.     }
  496.     else // fail
  497.     {
  498.         if (fi.error == errNoSuchPrefs)
  499.             return false;
  500.         else
  501.             fi.ReSignal();
  502.     }
  503. }
  504.  
  505. void PPrefsDatabase::SetAliasPrefs(OSType id, const FSSpec &const spec)
  506. {
  507.     AliasHandle ah = nil;
  508.     VOLATILE(ah);
  509.     FailInfo fi;
  510.     if (fi.Try())
  511.     {
  512.         FailOSErr(NewAlias(nil, spec, ah));
  513.         SetHandlePrefs(id, Handle(ah));
  514.         DisposeIfHandle(Handle(ah)); ah = nil;
  515.         fi.Success();
  516.     }
  517.     else // fail
  518.     {
  519.         DisposeIfHandle(Handle(ah)); ah = nil;
  520.         fi.ReSignal();
  521.     }
  522. }
  523.  
  524. void PPrefsDatabase::GetDirAliasPrefs(OSType id, FSSpec &spec)
  525. {
  526.     FSSpec tmpSpec;
  527.     GetAliasPrefs(id, tmpSpec);
  528.     CInfoPBRec info;
  529.     info.dirInfo.ioCompletion = nil;
  530.     info.dirInfo.ioNamePtr = tmpSpec.name;
  531.     info.dirInfo.ioVRefNum = tmpSpec.vRefNum;
  532.     info.dirInfo.ioFDirIndex = 0;
  533.     info.dirInfo.ioDrDirID = tmpSpec.parID;
  534.     FailOSErr(PBGetCatInfoSync(&info));
  535.     spec.parID = info.dirInfo.ioDrDirID;
  536.     spec.vRefNum = tmpSpec.vRefNum;
  537. }
  538.  
  539. void PPrefsDatabase::SetDirAliasPrefs(OSType id, const FSSpec &const spec)
  540. {
  541.     FSSpec tmpSpec;
  542.     tmpSpec.vRefNum = 0; // otherwise unused error
  543.     CInfoPBRec info;
  544.     info.dirInfo.ioCompletion = nil;
  545.     info.dirInfo.ioNamePtr = tmpSpec.name;
  546.     info.dirInfo.ioVRefNum = spec.vRefNum;
  547.     info.dirInfo.ioFDirIndex = -1;
  548.     info.dirInfo.ioDrDirID = spec.parID;
  549.     FailOSErr(PBGetCatInfoSync(&info));
  550.     tmpSpec.parID = info.dirInfo.ioDrParID;
  551.     tmpSpec.vRefNum = info.dirInfo.ioVRefNum;
  552.     SetAliasPrefs(id, tmpSpec);
  553. }
  554.  
  555. void PPrefsDatabase::GetSilentDirAliasPrefs(OSType id, FSSpec &spec, const OSType folderType)
  556. {
  557.     FailInfo fi;
  558.     if (fi.Try())
  559.     {
  560.         GetDirAliasPrefs(id, spec);
  561.         fi.Success();
  562.         return;
  563.     }
  564.     else // fail
  565.     {
  566.         switch (fi.error)
  567.         {
  568.             case nsvErr:
  569.             case fnfErr:
  570.             case paramErr:
  571.             case dirNFErr:
  572.             case errNoSuchPrefs:
  573.                 break; // == not found
  574.  
  575.             case userCanceledErr:
  576.                 Failure(0, 0); // maybe should just juse default folder
  577.                 
  578.             default:
  579.                 fi.ReSignal();
  580.         }
  581.     }
  582.     short errNo;
  583.     errNo = FindFolder(kOnSystemDisk, folderType, kCreateFolder, spec.vRefNum, spec.parID);
  584.     if (errNo)
  585.         errNo = FindFolder(kOnSystemDisk, kDesktopFolderType, kCreateFolder, spec.vRefNum, spec.parID);
  586.     if (errNo)
  587.         errNo = FindFolder(kOnSystemDisk, kSystemFolderType, kCreateFolder, spec.vRefNum, spec.parID);
  588.         // just _want_ a folder
  589.     FailOSErr(errNo);
  590.     SetDirAliasPrefs(id, spec);
  591. }
  592.  
  593.  
  594. void PPrefsDatabase::SetWindowPosPrefs(OSType id, TWindow *window)
  595. {
  596.     SetVRectPrefs(id, VRect(window->fLocation, window->fLocation + window->fSize));
  597. }
  598.  
  599. void PPrefsDatabase::GetSilentWindowPosPrefs(OSType id, TWindow *window)
  600. {
  601.     FailInfo fi;
  602.     if (fi.Try())
  603.     {
  604.         VRect newFrame;
  605.         GetVRectPrefs(id, newFrame);
  606.         if (!window->fIsResizable)
  607.             newFrame[botRight] = newFrame[topLeft] + window->fSize;
  608.         window->SetFrame(newFrame, kRedraw);        
  609.         window->ForceOnScreen();
  610.         fi.Success();
  611.         return;
  612.     }
  613.     else // fail
  614.     {
  615.         if (fi.error != errNoSuchPrefs)
  616.             fi.ReSignal();
  617.     }
  618. }
  619.  
  620. void PPrefsDatabase::SetApplNameAndID(OSType id, OSType applID, const CStr255 &name)
  621. {
  622.     long size = ((name.Length() + 8) & ~7) + sizeof(OSType); // remember length byte
  623.     char buffer[sizeof(CStr255) + sizeof(OSType)];
  624.     *OSTypePtr(buffer) = applID;
  625.     BytesMove(&name, buffer + sizeof(OSType), name.Length() + 1);
  626.     SetPrefs(id, buffer, size);
  627. }
  628.  
  629. void PPrefsDatabase::GetApplNameAndID(OSType id, OSType &applID, CStr255 &name)
  630. {
  631.     Ptr p = ComputePrefsAddress(id);
  632.     applID = *OSTypePtr(p);
  633.     p += sizeof(OSType);
  634.     long len = CStringPtr(p)->Length();
  635.     DebugCheckSize(id, ((len + 8) & ~7) + sizeof(OSType));
  636.     BytesMove(p, &name, len + 1);
  637. }
  638.  
  639. void PPrefsDatabase::DumpPrefs()
  640. {
  641. #if qDebug
  642.     const long maxDump = 50;
  643.     unsigned char buffer[maxDump + 10];
  644.     char info[100];
  645.     fprintf(stderr, "Dump of preferences database:\n");
  646.     ArrayIndex noPrefs = fPrefsTypes->GetSize();
  647.     for (ArrayIndex index = 1; index <= noPrefs; index++)
  648.     {
  649.         OSType id = fPrefsTypes->At(index);
  650.         fprintf(stderr, "%6ld: ", index);
  651.         fprintf(stderr, "%c%c%c%c", char(id >> 24 & 255), char(id >> 16 & 255), char(id >> 8 & 255), char(id & 255));
  652.         long elemSize = fPrefsDB->GetElementSize(index);
  653.         info[0] = 0;
  654.         if (elemSize == 4)
  655.         {
  656.             long lw = *( (long*) fPrefsDB->ComputeAddress(index));
  657.             if (lw <= 65535 && lw > -1000)
  658.                 sprintf(info, "%4ld", lw);
  659.         }
  660.         long dumpSize = Min(maxDump, elemSize);
  661.         BytesMove(fPrefsDB->ComputeAddress(index), buffer, dumpSize);
  662.         buffer[dumpSize] = 0;
  663.         unsigned char *p = buffer;
  664.         for (long i = 1; i <= dumpSize; i++)
  665.         {
  666.             if (*p < 32)
  667.                 *p = '.';
  668.             ++p;
  669.         }
  670.         fprintf(stderr, "%6ld %4s '%s'\n", elemSize, info, buffer);
  671.     }
  672.     fprintf(stderr, "End of dump\n");
  673. #endif
  674. }
  675.